﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enterprise.DataFactory;
using Enterprise.Model.ZJRB;
using System.Data;

namespace Enterprise.Data.ZJRB
{
    public class ZJRBData
    {

        public ZJRBData() { }

        #region  Method


        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string ZJRQ, string JH)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from ZJRB");
            strSql.Append(" where ZJRQ='" + ZJRQ + "' and JH='" + JH + "' ");
            return DbExecute.Exist(strSql.ToString());
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Enterprise.Model.ZJRB.ZJRBEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder strSql1 = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();
            if (model.ZJRQ != null)
            {
                strSql1.Append("ZJRQ,");
                strSql2.Append("'" + model.ZJRQ + "',");
            }
            if (model.JH != null)
            {
                strSql1.Append("JH,");
                strSql2.Append("'" + model.JH + "',");
            }
            if (model.ZJJC != null)
            {
                strSql1.Append("ZJJC,");
                strSql2.Append("" + model.ZJJC + ",");
            }
            if (model.ZJRB != null)
            {
                strSql1.Append("ZJRB,");
                strSql2.Append("'" + model.ZJRB + "',");
            }
            if (model.BZSM != null)
            {
                strSql1.Append("BZSM,");
                strSql2.Append("'" + model.BZSM + "',");
            }
            if (model.ZJZT != null)
            {
                strSql1.Append("ZJZT,");
                strSql2.Append("'" + model.ZJZT + "',");
            }
            strSql.Append("insert into ZJRB(");
            strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
            strSql.Append(")");
            int rows = DbExecute.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Enterprise.Model.ZJRB.ZJRBEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update ZJRB set ");
            if (model.ZJJC != null)
            {
                strSql.Append("ZJJC=" + model.ZJJC + ",");
            }
            else
            {
                strSql.Append("ZJJC= null ,");
            }
            if (model.ZJRB != null)
            {
                strSql.Append("ZJRB='" + model.ZJRB + "',");
            }
            else
            {
                strSql.Append("ZJRB= null ,");
            }
            if (model.BZSM != null)
            {
                strSql.Append("BZSM='" + model.BZSM + "',");
            }
            else
            {
                strSql.Append("BZSM= null ,");
            }
            if (model.ZJZT != null)
            {
                strSql.Append("ZJZT='" + model.ZJZT + "',");
            }
            else
            {
                strSql.Append("ZJZT= null ,");
            }
            int n = strSql.ToString().LastIndexOf(",");
            strSql.Remove(n, 1);
            strSql.Append(" where ZJRQ='" + model.ZJRQ + "' and JH='" + model.JH + "' ");
            int rowsAffected = DbExecute.ExecuteSql(strSql.ToString());
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string ZJRQ, string JH)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ZJRB ");
            strSql.Append(" where ZJRQ='" + ZJRQ + "' and JH='" + JH + "' ");
            int rowsAffected = DbExecute.ExecuteSql(strSql.ToString());
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Enterprise.Model.ZJRB.ZJRBEntity GetModel(string ZJRQ, string JH)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  ");
            strSql.Append(" ZJRQ,JH,ZJJC,ZJRB,BZSM,ZJZT ");
            strSql.Append(" from ZJRB ");
            strSql.Append(" where ZJRQ='" + ZJRQ + "' and JH='" + JH + "' ");
            Enterprise.Model.ZJRB.ZJRBEntity model = new Enterprise.Model.ZJRB.ZJRBEntity();
            DataSet ds = DbExecute.GetDataSet(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Enterprise.Model.ZJRB.ZJRBEntity DataRowToModel(DataRow row)
        {
            Enterprise.Model.ZJRB.ZJRBEntity model = new Enterprise.Model.ZJRB.ZJRBEntity();
            if (row != null)
            {
                if (row["ZJRQ"] != null)
                {
                    model.ZJRQ = row["ZJRQ"].ToString();
                }
                if (row["JH"] != null)
                {
                    model.JH = row["JH"].ToString();
                }
                if (row["ZJJC"] != null && row["ZJJC"].ToString() != "")
                {
                    model.ZJJC = decimal.Parse(row["ZJJC"].ToString());
                }
                if (row["ZJRB"] != null)
                {
                    model.ZJRB = row["ZJRB"].ToString();
                }
                if (row["BZSM"] != null)
                {
                    model.BZSM = row["BZSM"].ToString();
                }
                if (row["ZJZT"] != null)
                {
                    model.ZJZT = row["ZJZT"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ZJRQ,JH,ZJJC,ZJRB,BZSM,ZJZT ");
            strSql.Append(" FROM ZJRB ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbExecute.GetDataSet(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM ZJRB ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbExecute.ExecuteScalar(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.JH desc");
            }
            strSql.Append(")AS Row, T.*  from ZJRB T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbExecute.GetDataSet(strSql.ToString());
        }

        /*
        */

        #endregion  Method
        #region  MethodEx

        #endregion  MethodEx
    }
}
